package com.tgpay.iot.service;

import com.tgpay.iot.dto.DeviceInfoDto;
import com.tgpay.iot.entity.Device;
import com.tgpay.iot.entity.ChargingOrder;
import com.tgpay.iot.repository.DeviceRepository;
import com.tgpay.iot.repository.ChargingOrderRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * 设备服务层
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class DeviceService {
    
    private final DeviceRepository deviceRepository;
    private final ChargingOrderRepository chargingOrderRepository;
    
    /**
     * 获取设备列表
     */
    public List<DeviceInfoDto> getDeviceList() {
        List<Device> devices = deviceRepository.findAll();
        List<DeviceInfoDto> deviceInfoList = new ArrayList<>();
        
        for (Device device : devices) {
            DeviceInfoDto deviceInfo = convertToDeviceInfoDto(device);
            deviceInfoList.add(deviceInfo);
        }
        
        return deviceInfoList;
    }
    
    /**
     * 根据IMEI获取设备详情
     */
    public DeviceInfoDto getDeviceDetail(String imei) {
        Optional<Device> deviceOpt = deviceRepository.findByImei(imei);
        if (deviceOpt.isEmpty()) {
            throw new RuntimeException("设备不存在");
        }
        
        return convertToDeviceInfoDto(deviceOpt.get());
    }
    
    /**
     * 检查设备是否存在
     */
    public boolean deviceExists(String imei) {
        return deviceRepository.existsByImei(imei);
    }
    
    /**
     * 转换为设备信息DTO
     */
    private DeviceInfoDto convertToDeviceInfoDto(Device device) {
        DeviceInfoDto deviceInfo = new DeviceInfoDto();
        deviceInfo.setImei(device.getImei());
        deviceInfo.setName(device.getName());
        deviceInfo.setLocation(device.getLocation());
        deviceInfo.setStatus(device.getStatus());
        
        // 生成端口信息
        List<DeviceInfoDto.PortInfoDto> ports = new ArrayList<>();
        for (int i = 1; i <= device.getPortCount(); i++) {
            DeviceInfoDto.PortInfoDto port = new DeviceInfoDto.PortInfoDto();
            port.setPortId(i);
            
            // 检查端口是否正在充电
            Optional<ChargingOrder> chargingOrder = chargingOrderRepository
                .findByDeviceImeiAndPortIdAndStatus(device.getImei(), i, 1);
            
            if (chargingOrder.isPresent()) {
                port.setStatus(1); // 充电中
                port.setCurrentOrderNo(chargingOrder.get().getOrderNo());
            } else {
                port.setStatus(0); // 空闲
            }
            
            ports.add(port);
        }
        
        deviceInfo.setPorts(ports);
        return deviceInfo;
    }
    
    /**
     * 初始化测试数据
     */
    public void initTestData() {
        if (deviceRepository.count() == 0) {
            Device device = new Device();
            device.setImei("868123456789012");
            device.setName("测试充电桩001");
            device.setLocation("测试地点A");
            device.setStatus(1); // 在线
            device.setPortCount(4);
            deviceRepository.save(device);
            
            log.info("初始化测试设备数据完成");
        }
    }
}